package com.shenhonghui.twins;

import com.shenhonghui.twins.slice.PianoAbilitySlice;
import com.shenhonghui.twins.utils.PianoMusic;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.event.notification.NotificationRequest;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.InnerEvent;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;

public class PlayAbility extends Ability {
    private PianoMusic pianoMusic;
    public static final int PLAY_MUSIC_MSG = 100;
    public static final int SOUND_TYPE = 101;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        pianoMusic = new PianoMusic(getContext());
        NotificationRequest request = new NotificationRequest(1005);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("Twins").setText("Music");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);
        keepBackgroundRunning(1005,request);
    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelBackgroundRunning();
    }

    @Override
    protected IRemoteObject onConnect(Intent intent) {
        super.onConnect(intent);
        return new MusicRemountObject("MusicRemountObject").asObject();
    }

    @Override
    protected void onDisconnect(Intent intent) {
        super.onDisconnect(intent);
    }

    private class MusicRemountObject extends CrossDevicePlayerInterfaceStub {
        public MusicRemountObject(String descriptor) {
            super(descriptor);
        }

        @Override
        public void sendCommand(int command, int soundId) throws RemoteException {
            switch (command) {
                case PLAY_MUSIC_MSG:
                    pianoMusic.soundPlay(soundId);
                    break;
                case SOUND_TYPE:
                    PianoAbilitySlice.getHandler().sendEvent(soundId);
                    break;
                default:
                    break;
            }
        }
    };
}
